Skip to content

fix(codex): forward tool input that only arrives whole on an output item - #1215

Merged
jatmn merged 1 commit into
mainfrom
fix/codex-forward-tool-input
Nov 9, 2026
Merged

jatmn merged 1 commit into
mainfrom
fix/codex-forward-tool-input

Conversation

@jatmn

@jatmn jatmn commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1194. Supersedes #1201.

This maintainer rebuild carries @oniisanlabs's change from #1201 onto current main (88ec97d5630be8721e0a3d956f5b198868d0da84) on a fresh branch in Twigpine/zero. The original author remains the Git author; @jatmn is the committer. The rebuild commit's patch is byte-for-byte identical to the original PR's net diff against its merge-base.

Original implementation, tests, and bug report credit: @oniisanlabs. Source commit: b6ba46b0 on oniisanlabs/fix/codex-final-tool-arguments (via fork head for #1201). Review thread and CI history: #1201.


Summary

The Codex Responses stream handler kept tool input that arrived only on a whole output item for history replay, but never forwarded it to the runtime as a tool-call delta. A function_call whose arguments exist only on response.output_item.done, or a custom_tool_call whose input is already on response.output_item.added, therefore reached the agent with an empty argument string and failed with errors such as Invalid arguments for read_file: path is required.

Every builder write now goes through one forwardInput helper that also emits the delta, so toolCallBuilder.Arguments holds exactly what the runtime has received. The added handler forwards whole custom input, the delta handler forwards each fragment, and the done handler forwards the final input only when nothing has reached the runtime yet, so streamed fragments are never duplicated. The check for what still needs forwarding is therefore a check on what was actually sent, as requested on the issue; the invariant is documented on the field.

Linked issue

Closes #1194

Verification

From this branch on macOS arm64, using the Go version from go.mod:

  • make fmt-check
  • go vet ./...
  • go test ./...
  • go test -race ./internal/providers/openai -count=1
  • go run ./cmd/zero-release build
  • go run ./cmd/zero-release smoke
  • make vulncheck
  • git diff HEAD --check
  • make lint-static: the four pre-existing staticcheck findings (installtest, proxydial twice, tools/web_fetch), no new findings

One unrelated failure in the full suite: internal/tui TestRelativeAgeFormatsLastActivity fails identically on origin/main when the host timezone is not UTC and passes with TZ=UTC. Not touched here.

Regression evidence

TestCodexOutputItemDoneForwardsUnstreamedArguments drives the handlers directly across function/custom calls, with and without the added item, streamed or final-only input, plus custom input carried on the added item. TestCodexStreamCollectsToolInputDeliveredWhole drives the same shapes through StreamCompletion and zeroruntime.CollectStream, which is what the agent loop consumes. With the production file restored to main, both fail:

--- FAIL: TestCodexOutputItemDoneForwardsUnstreamedArguments/function/added/final-only
    arguments at tool-call-end = "", want "{\"path\":\"README.md\"}"
--- FAIL: TestCodexOutputItemDoneForwardsUnstreamedArguments/function/no-added/final-only
--- FAIL: TestCodexOutputItemDoneForwardsUnstreamedArguments/custom/added/final-only
--- FAIL: TestCodexOutputItemDoneForwardsUnstreamedArguments/custom/no-added/final-only
--- FAIL: TestCodexOutputItemDoneForwardsUnstreamedArguments/custom/input-on-added
    arguments at tool-call-end = "", want "*** Begin Patch\n*** Add File: hello.txt\n+hello\n*** End Patch\n"
--- FAIL: TestCodexStreamCollectsToolInputDeliveredWhole
    function call = {ID:fc-1 ProviderCallID:call-1 Name:read_file Arguments: Freeform:false}, want read_file with the done item's arguments
    custom call = {ID:ct-1 ProviderCallID:call-2 Name:apply_patch Arguments: Freeform:true}, want apply_patch with the added item's input

The four streamed cases pass with and without the fix, which pins that already-delivered input is not sent twice.

Checklist

  • The linked issue already has the issue-approved label.
  • go build ./..., go vet ./..., and go test ./... pass locally (see the unrelated timezone note above).
  • gofmt clean.
  • Tests added/updated for the change (and run under -race where relevant).
  • UI changes include screenshots or a short recording where possible. (No UI change.)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of tool-call input so it is reliably delivered even when it arrives outside of streamed delta events.
    • Prevented duplicate tool-call input from appearing in streamed updates or replayed history.
    • Preserved function-call arguments and custom-tool input when they arrive through final or initial item events.

Made with Cursor

The Codex Responses handler kept arguments that arrived only on
response.output_item.done (and custom tool input that arrived on
response.output_item.added) in the builder for history replay, but never
emitted them as tool-call deltas, so the runtime executed the call with an
empty argument string ("path is required").

Route every builder write through one forwardInput helper that also emits
the delta, so the builder holds exactly what the runtime has received. The
added handler forwards whole custom input, the delta handler forwards each
fragment, and the done handler forwards the final input only when nothing
reached the runtime yet, so streamed fragments are never duplicated.

Handler-level coverage spans function and custom calls, with and without
the added item, streamed or final-only, plus custom input on the added
item; a second test drives the same shapes through StreamCompletion and
zeroruntime.CollectStream. Against the unfixed handler the final-only and
input-on-added cases report an empty argument string at tool-call-end.

Closes #1194

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 22:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jatmn jatmn self-assigned this Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Zero automated PR review

Verdict: No blockers found

Blockers

  • None found.

Validation

  • [pass] Diff hygiene: git diff --check
  • [pass] Tests: go test ./...
  • [pass] Build: go run ./cmd/zero-release build
  • [pass] Smoke build: go run ./cmd/zero-release smoke

Scope

Head: 58ebf5a6dee0
Changed files (2): internal/providers/openai/codex_responses.go, internal/providers/openai/codex_test.go

This deterministic review checks validation status and basic diff hygiene. A human reviewer still owns product judgment and design quality.

@coderabbitai

coderabbitai Bot commented Nov 9, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Codex Responses handler now forwards tool input through a single toolCallBuilder helper. Input that arrives only on output_item.added or output_item.done now reaches the runtime as tool-call deltas. Streamed input is not duplicated. New tests cover these cases.

Changes

Codex tool input forwarding

Layer / File(s) Summary
forwardInput helper and handler wiring
internal/providers/openai/codex_responses.go
forwardInput skips empty fragments, buffers non-empty ones, and emits tool-call deltas. The delta handler uses it. The added handler forwards custom-tool input when the buffer is empty. The done handler forwards complete arguments or custom-tool input when no input was buffered. Direct buffer writes are removed.
Regression tests
internal/providers/openai/codex_test.go
A table-driven test checks function and custom-tool input from deltas, the added item, or the done item. It asserts single delivery before tool-call-end and history replay. An end-to-end test checks that the collector keeps done-item function arguments and added-item custom-tool input.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related issues

Possibly related PRs

  • Twigpine/zero#1201: It adds the same forwardInput helper and the same tests in the same file. It addresses issue #1194. The two PRs duplicate each other.

Suggested reviewers: anandh8x, vasanthdev2004

Merge Risk: Minimal · up to 58ebf

The change is mergeable after normal checks; no concrete unresolved tool-input forwarding failure was established.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: forwarding tool input that arrives whole on an output item.
Linked Issues check Passed Issue #1194 has one active direct target and no coding requirement outside the Codex stream handler. The requirements are: (1) when tool input appears only on response.output_item.done, forward it as …
Out of Scope Changes check Passed All changed hunks trace to #1194.
- toolCallBuilder doc comment and new forwardInput helper: supporting refactor. It centralizes the write-and-emit step that the done-handler duplication check depends…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue #1194 has one active direct target and no coding requirement outside the Codex stream handler. The requirements are: (1) when tool input appears only on response.output_item.done, forward it as a tool-call delta before tool-call-end; (2) do not duplicate input already delivered through deltas; (3) cover function and custom/freeform calls, with or without response.output_item.added.

Code at head (internal/providers/openai/codex_responses.go):

  • Requirement 1: handleOutputItemDone now calls builder.forwardInput with the done item's Arguments (function) or Input (custom) when builder.Arguments.Len() == 0. This happens before the StreamEventToolCallEnd send.
  • Requirement 2: forwardInput is the only write path into builder.Arguments and emits the delta. The done handler forwards only when nothing has reached the runtime, so streamed fragments are not resent.
  • Requirement 3: the function path (done-only) and the custom path (done-only, or input on the added item via handleOutputItemAdded when Arguments is empty) both forward input. The added-item path was previously only buffered, not emitted as a delta, so it did not reach the runtime. It now does.

Tests (internal/providers/openai/codex_test.go) cover all nine combinations, including the eight in the issue, and check that deltas before tool-call-end equal the expected input exactly once and that replayed history matches. TestCodexStreamCollectsToolInputDeliveredWhole covers the same shapes through StreamCompletion and zeroruntime.CollectStream. The PR reports these fail on the pre-change production file. I did not run the tests. The pass verdict rests on the diff and the reported results.

No unmet coding requirement is evident.

Full details: Out of Scope Changes check

Explanation

All changed hunks trace to #1194.

  • toolCallBuilder doc comment and new forwardInput helper: supporting refactor. It centralizes the write-and-emit step that the done-handler duplication check depends on.
  • handleOutputItemAdded: forwards custom input from the added item as a delta when nothing is buffered. This is the custom added-item case in the issue.
  • handleToolInputDelta: uses forwardInput. Same behavior, except that empty Delta values no longer emit an empty StreamEventToolCallDelta. This is a minor side effect of the refactor. It does not change argument content and is not unrelated work.
  • handleOutputItemDone: forwards final-only input under the no-duplication guard. This is the core fix.
  • codex_test.go: the two added tests exercise only #1194 behavior.

The diff touches no other files and adds no unrelated features. The ToolCallID for forwarded events uses builder.ID instead of the former map key. The test asserts ToolCallID equals the item ID on every event, which supports that they match on the tested paths.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 88ec97d and 58ebf5a.

Files selected for processing: 2

  • internal/providers/openai/codex_responses.go
  • internal/providers/openai/codex_test.go

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 3 remain after this review.

@jatmn
jatmn merged commit 15ff170 into main Nov 9, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatGPT/Codex provider can drop tool arguments supplied only in the final event

3 participants